package com.pizza.gui.panele;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import com.pizza.gui.AdapterList;
import com.pizza.spring.rmi.Adres;
import com.pizza.spring.rmi.Produkt;
import com.pizza.spring.rmi.User;
import com.pizza.spring.rmi.start.StartClient;

public class KoszykPanel extends JPanel{
	JScrollPane scrollPane = new JScrollPane();
	JFrame jFrame;
	JPanel myPanel = this;
	 AdapterList adapter ;
	User user;
	Adres adres;
	ArrayList<Produkt>listaProduktow = new ArrayList<Produkt>();
	JButton zamow = new JButton("Zamow");
	JTextField adresZamowienia = new JTextField();
	JLabel newaddres = new JLabel("Adres:");
	public KoszykPanel(JFrame jFrame) {
		this.jFrame=jFrame;
		
		scrollPane.setHorizontalScrollBarPolicy(
	            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
	    scrollPane.setVerticalScrollBarPolicy(
	            ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
	    this.jFrame.getContentPane().add(scrollPane);
	    setLayout(null);
	    adapter= new AdapterList(500, 500,new ArrayList<Produkt>());
		adapter.setSize(500, 500);
		adapter.setVisible(true);
		adapter.setBounds(150, 50, 400,500);
		scrollPane.setBounds(160, 54, 330, 340);
		setVisible(false);
		scrollPane.setViewportView(adapter);
		setBounds(150, 0, 550, 550);
		add(zamow);
		zamow.setBounds(10, 5, 100, 25);
		adresZamowienia.setBounds(180,5, 150, 25);
		newaddres.setBounds(120, 5, 100, 25);
		add(newaddres);
		add(adresZamowienia);
		zamow.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if(adresZamowienia.getText().isEmpty())
					StartClient.fasada.zamow(user, adres, adres.getUlica()+" "+adres.getNumerDomu(), listaProduktow);
				else
					StartClient.fasada.zamow(user, adres, adresZamowienia.getText(), listaProduktow);
				for (Component component : adapter.getComponents()) {
					component.setVisible(false);
					myPanel.remove(component);
					listaProduktow.removeAll(listaProduktow);
				}
			}
		});
		
	}
	@Override
	public void setVisible(boolean aFlag) {
		// TODO Auto-generated method stub
		super.setVisible(aFlag);
		adapter.setVisible(aFlag);
		scrollPane.setVisible(aFlag);
	}
	 public JScrollPane getScrollPane() {
			return scrollPane;
		}
		 public AdapterList getAdapter() {
				return adapter;
			}
	public void addProduct(Produkt produkt) {
		System.out.println(listaProduktow.size());
		listaProduktow.add(produkt);
	}
	public void setAdres(Adres adres) {
		this.adres = adres;
	}
	public void setUser(User user) {
		this.user = user;
	}
}
